import Vue from 'vue';
import Vue2Filters from 'vue2-filters';
import {} from '../base/observer.js';
var wl = require("../components/winLocation.js");
var assist = require("./creditAssist.js");
var amountSelect = require("./amountSelectVue.js");
var RepayVue = require("./repayVue.js");
var InfoVue = require("./infoVue.js");
var commonDialog = require("./commonDialogVue.js");
var guideVue = require("./guideVue.js");
var confirmDialog = require("./confirmDialog.js");
var loadingVue = require("./loadingVue.js");
var finallyVue = require("./finallyVue.js");
var applyContractVue = require("./applyContractVue.js");
var baiPoint = require("../components/baidumapGetPosition.js");

/*import {
    getConfig
} from '../components/wxConfig.js';*/
var selectVuePage, repayVue, infoVue, cDialogVue, confirmVue, ldVue, fvue, mer_id, contractVue, guideVue1, guideVue2;
var latitude = "0.00",
    longitude = "0.00";
var eachPay = [];
var rateArray = [];
var commonData = {
    step: "1",
    showDialog: "0"
};

$(document).ready(function() {
    wl.checkBrowser("2");
    initPage();
});

// 查询是否工作时间
function getWorkingTime() {

    $.post(LFQCreateOrderServlet, {
        "servlet_type": "query_openday"
    }, function(data) {
        data = JSON.parse(data);
        console.dir(data);
        if (data.openday_response && data.openday_response.today) {
            window.nowTormatDate = data.openday_response.today.replace(/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})/, '$1/$2/$3 $4:$5:');
            window.nowTime = new Date(nowTormatDate);
            var hours = nowTime.getHours();
            var working_time = false;
            if (hours > 8 && hours < 17 /* && data.openday_response.isOpenday == 1*/ ) {
                $("#speak_btn").attr("class", "weui-btn weui-btn_primary");
                $("#speak_btn").text("立即申请");
            }
        } else { // 没请求到服务器时间

        }

    });
}


// 查询期数费率
function getRateState() {
    $.post(LFQCreateOrderServlet, {
        "servlet_type": "query_mer_info",
        "sceneId": sceneId,
        "web": 1
    }, function(rateDate) {
        rateDate = JSON.parse(rateDate);
        console.dir(rateDate);
        if (rateDate.lfq_merinfo_response && rateDate.lfq_merinfo_response.mer_prdt_rate_arrays) {
            mer_id = rateDate.lfq_merinfo_response.mer_id;
            var mer_prdt_rate = rateDate.lfq_merinfo_response.mer_prdt_rate_arrays.mer_prdt_rate;
            amountSelect.setLimit(mer_prdt_rate);
            amountSelect.setRateArray(mer_prdt_rate);
        }
    });
}

var returnMap = {
    "0364": "您所提供信用卡的有效额度不足，请查询有效额度后再试。",
    "0170": "您所提供信用卡的卡信息(姓名、身份证、手机号、有效期、CVN2等)错误，请确认后再试。",
    "0366": "您所提供信用卡的卡信息(姓名、身份证、手机号、有效期、CVN2等)错误，请确认后再试。",
    "0365": "您所提供信用卡的卡信息(姓名、身份证、手机号、有效期、CVN2等)错误，请确认后再试。",
    "0113": "您所提供信用卡的卡信息(姓名、身份证、手机号、有效期、CVN2等)错误，请确认后再试。",
    "0361": "您所提供信用卡的有卡号错误，请确认后再试。",
    "0119": "您所提供信用卡的有效期不足，请更换其他银联信用卡申请。",
    "0373": "您所提供信用卡的有效期不足，请更换其他银联信用卡申请。",
    "0117": "您所提供信用卡暂不支持本业务，请更换其他银联信用卡申请。",
    "0378": "您所提供信用卡暂不支持本业务，请更换其他银联信用卡申请。",
    "0332": "您所提供信用卡暂不支持本业务，请更换其他银联信用卡申请。",
    "0360": "您所提供信用卡暂不支持本业务，请更换其他银联信用卡申请。",
    "0330": "您所提供信用卡暂不支持本业务，请更换其他银联信用卡申请。",
    "0362": "您所提供信用卡暂不支持本业务，请更换其他银联信用卡申请。",
    "0144": "风险受限，请更换其他银联信用卡申请。",
    "0338": "风险受限，请更换其他银联信用卡申请。",
    "0141": "风险受限，请更换其他银联信用卡申请。"
};

//
function bindSubscribeShowConfirm() {
    $.subscribe("showConfirmDailog", function() {

        confirmVue.loanAmoun = selectVuePage.loanAmoun;
        confirmVue.stage = selectVuePage.checkedRateObject.stage;
        confirmVue.eachPay = selectVuePage.checkedRateObject.repayment;
        confirmVue.skcard = infoVue.cardId.replace(/(\d{4})(\d{4})(\d{4})/, "$1  $2  $3 ");
        confirmVue.xykno = infoVue.xykId.replace(/(\d{4})(\d{4})(\d{4})/, "$1  $2  $3 ");
        commonData.showDialog = 3;
    });

}

// 监听提交交易信息
function bindSubscribeSubmitInfo() {
    $.subscribe("submitInfo", function() {
        commonData.showDialog = 4;
        var parameters = {
            open_id: openid,
            id_no: infoVue.userId.replace(/\s/g, ""),
            hp_no: infoVue.phoneNo.replace(/\s/g, ""),
            cust_name: infoVue.name,
            lfq_cnt: selectVuePage.checkedRateObject.stage,
            lfq_amt: repayVue.amount,
            goods_amount: selectVuePage.loanAmoun,
            creditCard: infoVue.xykId.replace(/\s/g, ""),
            debitCard: infoVue.cardId.replace(/\s/g, ""),
            cvn2Num: infoVue.cvn2,
            timeNum: infoVue.timeNum,
            servlet_type: "create_order",
            merId: mer_id,
            cardFeeRate: selectVuePage.checkedRateObject.custRate,
            verify_code: infoVue.vcode,
            prdt_no: selectVuePage.checkedRateObject.prdtNo,
            scene_id: sceneId,
            lat: latitude,
            lng: longitude
        };

        console.log("submitInfo..parameters....");
        console.dir(parameters);



        $.post(LFQCreateOrderServlet, parameters, function(lfData) {
            console.log("lfData...........");
            commonData.showDialog = 0;
            lfData = JSON.parse(lfData);
            console.dir(lfData);

            if (lfData.lft_orderinfo_response) {
                commonData.step = 5;
                if (lfData.lft_orderinfo_response.resp_code == "0000") {
                    fvue.finallyCode = 1;

                } else if (returnMap[lfData.lft_orderinfo_response.resp_code]) {
                    fvue.finallyCode = 2;
                    fvue.finallTS = returnMap[lfData.lft_orderinfo_response.resp_code];
                } else {
                    fvue.finallTS = "请求超时,请稍后再试";
                }


            } else if (lfData.error_response && lfData.error_response.resp_msg) {
                commonData.showDialog = 1;
                cDialogVue.prompt = lfData.error_response.resp_msg;
            }

        });

    });
}


// 取短信验证码
function bindGetdxvCode() {
    $.subscribe("togetvcode", function() {
        var vcParments = {
            hp_no: infoVue.phoneNo,
            openid: openid,
            prdt_no: selectVuePage.checkedRateObject.prdtNo
        };

        $.post(LFQ_SMS_SEND_URL, vcParments, function(vcdata) {
            console.dir(vcdata);
        });

    });

}


// 绑定事件
function bindEvent() {

    $("#speak_btn").on("click", function() {
        var speakBtn = $(this);
        if (speakBtn.hasClass("weui-btn_disabled")) {
            return;
        } else {
            $("#speak").hide();
            commonData.step = 2;
            var creditAppFrist = localStorage.getItem("creditAppFrist");
            if (creditAppFrist != 2) {
                commonData.showDialog = 5;

            }
        }
    });

    $("#lock-cvn2").on("click", function() {
        var $infocvn2 = $("#info-cvn2");
        if ($infocvn2.attr("type") == "password") {
            $infocvn2.attr("type", "tel");
        } else {
            $infocvn2.attr("type", "password");
        }
    });
}

/*// 取微信配置
function getwxConfig() {
    getConfig(LFQ_PLACE_URL, {
        "mapUrl": "/creditLoan/apply.html" + location.search
    }, ['getLocation']);
}
*/
// 获取百度API经纬度
function getBaiduPoint() {
    
    $.subscribe("baiduGetPointEnd",function  () {
        var point = baiPoint.getPoint();
        latitude = point.lat;
        longitude = point.lng;
    });
}

// 页面初始化
function initPage() {
    /*    document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
            // 通过下面这个API隐藏右上角按钮
            wx.hideOptionMenu();
        });*/
    getWorkingTime();
    wl.QueryString.Initial();
    inintValue();
    getRateState();
    bindSubscribeSubmitInfo();
    bindSubscribeShowConfirm();
    bindGetdxvCode();
    //    getLocation();
    //    getwxConfig();
    bindEvent();
    getBaiduPoint(); // 取百度api经纬度
}

// 初始化变量
function inintValue() {
    window.sceneId = wl.QueryString.GetValue("scene_id");
    window.openid = "0201H5"; // wl.QueryString.GetValue("openid");  "0201H5"; 
    selectVuePage = amountSelect.createSelectVue(rateArray, commonData, Vue2Filters);
    repayVue = RepayVue.createRepayVue(eachPay, commonData);
    infoVue = InfoVue.createInfoVue(commonData);
    cDialogVue = commonDialog.createCommonDialog(commonData);
    guideVue1 = guideVue.createGuideVue(commonData, "#amountSelect-Dialog");
    guideVue2 = guideVue.createGuideVue(commonData, "#info-guide");
    confirmVue = confirmDialog.createConfirmDialog(commonData, Vue2Filters);
    ldVue = loadingVue.createLodingDialog(commonData);
    fvue = finallyVue.createfinallDialog(commonData);
    contractVue = applyContractVue.createapplyContractvue(commonData, function() {
        commonData.step = 2;
    });
}